--
-- Author: shunguo.chen
-- Date: 2019-05-24 14:25:47
--
-- RuneListRender 符文List render
--

local RuneIconRender = import(".RuneIconRender")

local string_format = string.format
local color_not_get = cc.c3b( 255, 105, 85 )

local ClassRef = app.mvc.createListViewCell(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RuneListRenderNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())

	self._runeIcon = RuneIconRender.create( self._ui.icon )
	self._runeIcon:setType(2)
	self._runeIcon:setClickEnable( false )

	self:setMaskVisible( false )

	self._ui.selected_btn:addClickEventListener(function()
		if self._data and self._cellModel then
			if self._data:getUserLv() > UD:getLevel() then
				display.pushToast( L("tips_2000118") )
				return
			end

			local skill = UD:getSkillByTeam(self._data:getSkillTeam())
			if not skill then
				display.pushToast( L("lua_code_text_283") )
				return
			end

			if self._data.isNew then
				UD:removeNewGetRune(self._data:getId())
				self._runeIcon:setDotVisible(false)
			end

			if self._data:isEquiped() then -- 已装备？
				self:dispatchCellEvent("rune_unequip", self)
			else
				self:dispatchCellEvent("rune_equip", self)
			end
		end
	end)

	display.uiAddClick(self._ui.Panel, function()
		if self._data and self._cellModel then
			if self._data.isNew then
				UD:removeNewGetRune(self._data:getId())
				self._runeIcon:setDotVisible(false)
			end
			self:dispatchCellEvent("rune_select", self)
		end
	end)

	self:refresh()
end

-- data = Rune
function ClassRef:refresh()
	if self._data then
		local rune = self._data

		self._runeIcon:setData( rune )
		self._ui.name:setString( rune:getName() )

		local isGot = rune:isGot()
		local isEquiped = rune:isEquiped()
		self:setMaskVisible(not isGot)
		self._ui.lock:setVisible(not isGot)
		self:setEquipBtnVisible(isGot)
		self._ui.bg_c:setVisible(isGot and isEquiped)
		self:setEquiped(isGot and isEquiped)
		self._runeIcon:setDotVisible(isGot and isNew)

		local lvStr
		if isGot then
			lvStr = isEquiped and L("lua_code_text_37") or L("lua_code_text_341")
		else
			lvStr = L("lua_code_text_359")
		end
		self._ui.lv:setTextColor(isGot and display.COLOR_BLACK or cc.c3b( 255, 105, 85 ))
		self._ui.lv:setString(lvStr)
	end
end

function ClassRef:getIconWorldPos()
	local worldPos = self._runeIcon:convertToWorldSpace(cc.p(0,0))
	return worldPos
end

-- 设置icon选中状态
function ClassRef:setIconSelected( b )
	self._runeIcon:setSelected( b )
end

-- 设置 装备 按钮可见性
function ClassRef:setEquipBtnVisible( b )
	self._ui.selected_btn:setVisible( b )
end

-- 设置 装备 状态
function ClassRef:setEquiped( b )
	self._ui.selected_btn:setBright( not b )
end

-- 设置 解锁状态 显示与否
function ClassRef:setMaskVisible( b )
	self._ui.mask:setVisible( b )
end

function ClassRef:onBtnClick(func)
	self._onClick = func
end

return ClassRef
